#include "Menu.h"

Menu::Menu()
	:m_shapeCon(ShapeContainer()), m_option(0)
{
	m_app = new Application(&m_shapeCon);
};

void Menu::selection() 
{
	switch (m_option)
	{
	case 1:
		chooseShapeToAdd();

		break;

	case 2:
		m_app->drawShapeCon();
		break;

	case 3:
		m_app->changeDefaultDraw();		
		break;

	case 4:
		m_app->select();		
		break;

	case 5:
		m_app->printAnimWithSplit();
		break;

	case 6:
		m_app->printAnimWithCollision();
		break;

	case 7:
		saveFileSelection();
		break;

	case 8:
		loadFileSelection();
		break;

	}
}

void Menu::addShapeSelection() 
{
	switch (m_option)
	{
	case 1:
		if (!m_app->addRectangle())
		{
			cerr << "Error: couldn't add a rectangle to the array." << endl;
			return;
		}
		break;

	case 2:
		if (!m_app->addCircle())
		{
			cerr << "Error: couldn't add a circle to the array." << endl;
			return;
		}
		break;

	case 3:
		return;
		break;
	}

		gotoxy(0,21);
		cout << "Added succesfully..." << endl;
}

void Menu::saveFileSelection() 
{
	char saveOption;
	string fileName;

	do
	{
		cout << "Please enter a method to save (b - binary, t - text): ";
		cin >> saveOption;
		saveOption = tolower(saveOption);
	}
	while (!legalMethod(saveOption));

	cout << "Please enter a file name: ";
	cin >> fileName;

	switch (saveOption)
	{
	case 'b':
		if (!m_app->saveBinFile(fileName))
		{
			cerr << "Error: couldn't save the data to the file." << endl;
			return;
		}
		break;

	case 't':
		if (!m_app->saveTextFile(fileName))
		{
			return;
		}
		break;
	}

		gotoxy(0,21);
		cout << "Saved succesfully..." << endl;
}

void Menu::checkIfAllSaved()
{
	char cont;

	// If the user is trying to load from file and haven't saved his current session,
	// a message will pop
	if (!m_shapeCon.getSaveStamp())
	{
		cout << "The data you are currently working on has not been saved ";
		cout << "and will be lost!" << endl;

		do
		{
			cout << "Do you wish to continue? (y - yes, n - no)" << endl;
			cin >> cont;
			cont = tolower(cont);
			cout << endl;
		}
		while (cont != 'y' && cont != 'n');

		switch (cont)
		{
		case 'y':
			break;

		case 'n':
			return;
			break;
		}
	}
	loadFileSelection();
}

void Menu::loadFileSelection() 
{
	char loadOption;
	string fileName;

	do
	{
		cout << "Please enter a method to load (b - binary, t - text): ";
		cin >> loadOption;
		loadOption = tolower(loadOption);
		cout << endl;
	}
	while (!legalMethod(loadOption));

	cout << "Please enter a file name: ";
	cin >> fileName;

	switch (loadOption)
	{
	case 'b':
		if (!m_app->loadBinFile(fileName))
		{
			cerr << "Error: couldn't load the data to the file." << endl;
			return;
		}
		break;

	case 't':
		if (!m_app->loadTextFile(fileName))
		{
			cerr << "Error: couldn't load the data to the file." << endl;
			return;
		}
		break;
	}
}

void Menu::drawMenu() const
{
	
	int defaultDraw = m_app->getDefaultDraw();

	clrscr();

	cout << "1. Add shape" << endl;

	// "else if" is used for future features
	if (defaultDraw == 0)
		cout << "2. Draw (not transparent)" << endl;
	else if (defaultDraw == 1)
		cout << "2. Draw (transparent)" << endl;

	cout << "3. Change default drawing option (1 - not transparent, 2 - transparent)" << endl;

	cout << "4. Select a shape" << endl;

	cout << "5. Show animation with screen split display" << endl;

	cout << "6. Show animation with collision" << endl;

	cout << "7. Save to file" << endl;

	cout << "8. Read from file" << endl;

	cout << "9. Exit" << endl;

}

void Menu::chooseShapeToAdd()
{
	do
	{
		clrscr();
		cout << "1. Add rectangle" << endl;
		cout << "2. Add circle" << endl;
		cout << "3. Cancel" << endl;
		getInput();
	}
	while (!legalAddShape(m_option));

	addShapeSelection ();

	gotoxy(0,22);
	cout << "Press any key to continue..." << endl;
}

void Menu::chooseWriteMethod()
{
	do
	{
		clrscr();
		cout << "1. Save to a binary file" << endl;
		cout << "2. Save to a text file" << endl;
		cout << "3. Cancel" << endl;
		getInput();
	}
	while (!legalSaveFile(m_option));

	saveFileSelection ();

	gotoxy(0,22);
	cout << "Press any key to continue..." << endl;
}

bool Menu::legal(unsigned int option) const
{
	if (option == 1 || 
		option == 2 ||
		option == 3 ||
		option == 4 ||
		option == 5 ||
		option == 6 ||
		option == 7 ||
		option == 8 ||
	// assuming the class can be used for different applications
		option == 9)
		return true;
	
	return false;
}

bool Menu::legalAddShape(unsigned int option) const
{
	if (option == 1 || 
		option == 2 ||
		option == 3)
		return true;
	
	return false;
}

bool Menu::legalSaveFile(unsigned int option) const
{
	if (option == 1 || 
		option == 2 ||
		option == 3)
		return true;
	
	return false;
}

bool Menu::legalMethod(char option) const
{
	if (option == 'b' ||  
		option == 't' )
		return true;
	
	return false;
}

void Menu::run() 
{

	do
	{
		drawMenu();
		getInput();

		if (legal(m_option))
		{
			selection();
		}
		else
			cout << endl << "You have entered an incorrect selection" << endl;

		if (!exit())
		{
			gotoxy(0,22);
			cout << "Press any key to continue..." << endl;
			char temp = _getch();
		}
		else
			cout << "Bye Bye..." << endl;
	}
	while (!exit());

}

Menu::~Menu ()
{
	delete m_app;

}